# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/1/22
# @File   : TX08_TaxRating.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:纳税评级
code:TX08_TaxRating
tableName:
columnName:
groups:税务模块
dependencies:TX_CQ_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    '''
    获取表
    :param x:
    :return:
    '''
    try:
        ic_data = TX_CQ_DSJ['data'].get(table_Name)
        return ic_data if isinstance(ic_data, list) and len(ic_data) > 0 else [{}]
    except:
        return [{}]

def isfloat(x):
    '''
    判断x为float类型
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float):
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True', 'NAN', 'nan',
                     'False', '-inf', '-INF', '-INFINITY', '-infinity', 'NaN', 'Nan']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def TX08_TaxRating():
    try:
        #税务评级信息
        syptSwpjxx = getTable('syptSwpjxx')
        if syptSwpjxx == [{}]:
            return u'缺失值'
        else:
            #存税务评级 年份和评级
            pj_tmp = {}
            for i in syptSwpjxx:
                if i.get('SWXYPJSJ') not in null_type_list and isfloat(i.get('SWXYPJSJ'))\
                        and i.get('SWXYPJ') in ['A', 'B', 'C', 'D', 'M']:
                    pj_year = float(i.get('SWXYPJSJ'))
                    pj_tmp.update({pj_year:i.get('SWXYPJ')})
            #判断评级信息
            month = datetime.datetime.now().month
            year = datetime.datetime.now().year
            if pj_year==year:
                result = pj_tmp.get(year)
                return result if result is not None else u'缺失值'
            elif pj_year!=year and month >= 1 and month <= 5:
                if not pj_tmp.get(year-1):
                    result = pj_tmp.get(year-2)
                    return result if result is not None else u'缺失值'
                else:
                    return pj_tmp.get(year-1)
            elif pj_year!=year and month >=6 and month <= 12:
                result = pj_tmp.get(year-1)
                return result if result is not None else u'缺失值'
            else:
				assert False
    except:
        return u'缺失值'

result = TX08_TaxRating()
